package com.yyk.yykywd.mvvm.ui

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.R
import com.yyk.yykywd.base.BaseActivity
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.getViewModel
import com.yyk.yykywd.hideRefreshAndLoadMore
import com.yyk.yykywd.mvvm.adapter.MaterialAdapter
import com.yyk.yykywd.mvvm.entity.MaterialBean
import com.yyk.yykywd.mvvm.view_model.MaterialViewModel
import com.yyk.yykywd.showStateLayout
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_marketing_material.*
import kotlinx.android.synthetic.main.activity_marketing_material.srl
import kotlinx.android.synthetic.main.layout_load.*
import kotlinx.android.synthetic.main.layout_load.loading_layout

class MarketingMaterialActivity : BaseActivity() {
    private lateinit var viewModel: MaterialViewModel
    private lateinit var materialAdapter: MaterialAdapter

    override fun initView() {
        setTitle("营销素材")
        setBackArrow()
        setMainTitleRightText("添加素材") {
            viewModel.checkPermission()
        }
        viewModel = getViewModel(this, MaterialViewModel::class.java)

        materialAdapter = MaterialAdapter(mActivity, R.layout.item_material, viewModel.mMaterials)
        with(rv) {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = materialAdapter
        }
    }

    override fun initData() {
        loading_layout.showLoading()
    }

    override fun onResume() {
        super.onResume()
        reloadData()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_marketing_material
    }

    override fun setListener() {
        super.setListener()
        viewModel.mMaterial.observe(this, Observer<MaterialBean> {
            hideRefreshAndLoadMore(srl)
            showStateLayout(materialAdapter, it.code, viewModel.mMaterials, it.msg, loading_layout)
        })

        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.getMaterials()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                reloadData()
            }
        })

        materialAdapter.setOnItemChildClickListener { _, _, position ->
            viewModel.delMaterial(viewModel.mMaterials[position].id.toString())
        }

        viewModel.base.observe(this, Observer<BaseBean> {
            if (it.code != 200) {
                ToastUtil.showShort(it.msg)
                return@Observer
            }
            ToAnotherActivity.toCommonActivity(mActivity, AddMaterialActivity::class.java)
        })

        viewModel.mDelSuccess.observe(this,Observer<BaseBean>{
            ToastUtil.showShort(it.msg)
            if (it.code == 200) {
                reloadData()
            }
        })
    }

    private fun reloadData(){
        viewModel.refresh(viewModel.mMaterials)
        viewModel.getMaterials()
    }
}
